/**
 * Created by 33183 on 2017/12/7.
 */
//将url参数转换成hashmap对象
function paramToHashMap(param) {
    var tmpArr = param.split("&");
    var hashMap = new Object();
    for ( var i = 0; i < tmpArr.length; i++) {
        var para = tmpArr[i];
        if (para.indexOf("=") != -1) {
            var kv = para.split("=");
            hashMap[kv[0]] = kv[1];
        }
    }
    return hashMap;
}
 var params = paramToHashMap(document.location.search.split("?")[1]);
 var stationNo = decodeURIComponent(params["stationNo"]);
 var CountyName = decodeURIComponent(params["CountyName"]);

$(function() {
    load();
    $("input[name='tabs']").click(function(){
        load();
    })
});

//初始化加载曲线跟表格数据
function load(){
    var index = layer.load(1);

    var foreData = new Array();//预测数据
    var foreDataLTemp = new Array();//预测数据
    var foreDataHTemp = new Array();//预测数据
    var date = new Array(); //所有日期
    var obj = $("input[name='tabs']:checked");
    var factor = $('label[for=' + obj.attr('id') + ']').text();
    var p = {
        "factor": encodeURIComponent(factor),
        "cityName": encodeURIComponent(CountyName)
    };
    $.ajax({
        url: "../metCityForecast/temperatureForecastQuery",
        type: "get",
        contentType: "application/json",
        dataType : "json",
        data : p/*JSON.stringify(p)*/,//转换成字符串。后台使用了RequestBody注解
        success : function(data) {
            if(obj.attr('id')==='one'){
                $.each(data.temp, function (index, item) {
                    var time = new Date(parseInt(item.foreTime)).Format("MM-dd hh:mm");
                    foreData.push(item.predictionVal);
                    date.push(time + item.tempIcon); //日期
                });
                $.each(data.lTemp, function (index, item) {
                    foreDataLTemp.push(item.predictionVal);
                });
                $.each(data.hTemp, function (index, item) {
                    foreDataHTemp.push(item.predictionVal);
                });
            }else {
                $.each(data.yctemperaturelist, function (index, item) {
                    var time = new Date(parseInt(item.foreTime)).Format("MM-dd hh:mm");
                    foreData.push(item.predictionVal);
                    date.push(time);
                });
            }
            if(obj.attr('id')==='one')
                lodaTuTemp(foreData, foreDataLTemp, foreDataHTemp, data.minTemp, data.maxTemp, date);
            else if(obj.attr('id')==='two')
                lodaTuPre(foreData, date);
            else if(obj.attr('id')==='three')
                lodaTuHum(foreData, date);
            else if(obj.attr('id')==='four')
                lodaTuSpeed(foreData, date);
            layer.close(index);
        }
    });
}
//折线图 日期，实况功率，图例，实况风速，预报功率
function lodaTuTemp(foreData, foreDataLTemp, foreDataHTemp, minValue, maxValue, date) {
    $("#main").show();
    $("#mainone").hide();
    $("#maintwo").hide();
    $("#mainthree").hide();
    var myChart = echarts.init(document.getElementById("main"));
    var colors = ['#6eb2ff', '#98FB98', '#FF0000'];
    option = {
        backgroundColor: '#071f3a',//背景色
        color: colors,
        tooltip: {
            trigger: 'axis',
            axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                type : 'line'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        legend: {
            show: true,
            //orient:'horizontal',
            //borderWidth:2,
            selected: {
                '预报低温': false,
                '预报高温': false,
            },
            data: [
                {
                    name: '预报温度',
                    textStyle: {
                        fontSize: 12,
                        fontWeight: 'bolder',
                        color: '#fff'
                    },
                },
                {
                    name:'预报低温',
                    textStyle:{
                        fontSize:12,
                        fontWeight:'bolder',
                        color:'#fff'
                    },
                },
                {
                    name:'预报高温',
                    textStyle:{
                        fontSize:12,
                        fontWeight:'bolder',
                        color:'#fff'
                    },
                }
            ]
        },
        toolbox: {
            show: true,
            feature: {
            }
        },
        calculable: false,//取消拖拽
        dataZoom: {
            show: true,
            realtime: true,
            start: 0,
            end: 20
        },
        xAxis: [
            {
                //interval:0,//X轴的刻度间隔
                splitLine: {show: false},//去除网格线
                type: 'category',
                data: date,
                boundaryGap: true,

                //data: date,
                axisLabel: {
                    formatter: '{value}',
                    textStyle: {
                        fontWeight: "bolder",
                        color: "#fff"
                    }
                }
            }
        ],
        yAxis: [
            {
                splitLine: {show: true},//去除网格线
                name: '摄氏度(℃)',
                min: minValue,
                max: maxValue,
                nameTextStyle: {
                    color: '#fff'
                },
                type: 'value',
                axisLabel: {
                    formatter: '{value}'/*+'mw'*/,
                    textStyle: {
                        fontWeight: "bolder",
                        color: "#fff"
                    }
                }
            }
        ],
        series: [
            {
                name: '预报温度',
                type: 'line',
                smooth: true,
                yAxisIndex: 0,
                //symbol: 'none',  //这句就是去掉点的
                data: foreData
            }, {
                name: '预报低温',
                type: 'line',
                smooth: true,
                yAxisIndex: 0,
               // symbol: 'none',  //这句就是去掉点的
                data: foreDataLTemp
            }, {
                name: '预报高温',
                type: 'line',
                smooth: true,
                yAxisIndex: 0,
                data: foreDataHTemp,
                //symbol: 'none',  //这句就是去掉点的
                itemStyle: {
                    normal: {
                        lineStyle: {
                            shadowBlur: 10,
                            shadowColor: 'rgba(0, 0, 0, 0.5)',
                            smooth: true,
                            width: 2//线宽
                        }
                    }
                }

            }
        ]
    };
    myChart.setOption(option);
}

function lodaTuPre(foreData, date) {
    $("#main").hide();
    $("#mainone").show();
    $("#maintwo").hide();
    $("#mainthree").hide();
    var myChart = echarts.init(document.getElementById("mainone"));
    var colors = ['#FF0000'];
    option = {
        backgroundColor: '#071f3a',//背景色
        color: colors,
        tooltip: {
            trigger: 'axis',
            axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        legend: {
            show:true,
            data:[
                {
                    name:'预报降水',
                    textStyle:{
                        fontSize:12,
                        fontWeight:'bolder',
                        color:'#fff'
                    },
                }
            ]
        },
        grid: {
            left: '5%',
            right: '5%',
            bottom: '5%',
            y: 30,
            containLabel: true
        },
        calculable: false,//取消拖拽
        dataZoom: {
            show: true,
            realtime: true,
            start: 0,
            end: 20
        },
        xAxis: [
            {
                //interval:0,//X轴的刻度间隔
                splitLine: {show: false},//去除网格线
                type: 'category',
                data: date,
                boundaryGap: true,

                axisLabel: {
                    formatter: '{value}',
                    textStyle: {
                        fontWeight: "bolder",
                        color: "#fff"
                    }
                }
            }
        ],
        yAxis: [
            {
                splitLine:{show: false},//去除网格线
                name: '降水量(mm)',
                min:0,
                nameTextStyle:{
                    color:'#fff'
                },
                type: 'value',
                axisLabel:{
                    formatter: '{value}'/*+'mw'*/,
                    textStyle:{
                        fontWeight:"bolder",
                        color:"#fff"
                    }
                }
            }
        ],
        series: [
            {
                name: '预报降水',
                type:'bar',
                barWidth: '80%',
                yAxisIndex: 0,
                data: foreData,
                symbol:'none',  //这句就是去掉点的
                itemStyle:{
                    normal:{
                        lineStyle:{
                            shadowBlur: 10,
                            shadowColor: 'rgba(0, 0, 0, 0.5)',
                            smooth:true,
                            width: 2//线宽
                        }
                    }
                }

            }
        ]
    };
    myChart.setOption(option);
}

function lodaTuHum(foreData, date) {
    $("#main").hide();
    $("#mainone").hide();
    $("#maintwo").show();
    $("#mainthree").hide();
    var myChart = echarts.init(document.getElementById("maintwo"));
    var colors = ['#FF0000'];
    option = {
        backgroundColor: '#071f3a',//背景色
        color: colors,
        tooltip: {
            trigger: 'axis',
            axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                type : 'line'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        legend: {
            show:true,
            //orient:'horizontal',
            //borderWidth:2,
            data:[
                {
                    name:'预报湿度',
                    textStyle:{
                        fontSize:12,
                        fontWeight:'bolder',
                        color:'#fff'
                    },
                }
            ]
        },
        toolbox: {
            show: true,
            feature: {
            }
        },
        calculable: false,//取消拖拽
        dataZoom: {
            show: true,
            realtime: true,
            start: 0,
            end: 20
        },
        xAxis: [
            {
                //interval:0,//X轴的刻度间隔
                splitLine: {show: false},//去除网格线
                type: 'category',
                data: date,
                boundaryGap: true,

                //data: date,
                axisLabel: {
                    formatter: '{value}',
                    textStyle: {
                        fontWeight: "bolder",
                        color: "#fff"
                    }
                }
            }
        ],
        yAxis: [
            {
                splitLine:{show: false},//去除网格线
                name: '水汽压(hPa)',
                min:0,
                nameTextStyle:{
                    color:'#fff'
                },
                type: 'value',
                axisLabel:{
                    formatter: '{value}'/*+'mw'*/,
                    textStyle:{
                        fontWeight:"bolder",
                        color:"#fff"
                    }
                }
            }
        ],
        series: [
            {
                name: '预报湿度',
                type: 'line',
                smooth: true,
                yAxisIndex: 0,
                data: foreData,
                //symbol: 'none',  //这句就是去掉点的
                itemStyle: {
                    normal: {
                        lineStyle: {
                            shadowBlur: 10,
                            shadowColor: 'rgba(0, 0, 0, 0.5)',
                            smooth: true,
                            width: 2//线宽
                        }
                    }
                }

            }
        ]
    };
    myChart.setOption(option);
}

function lodaTuSpeed(foreData, date) {
    $("#main").hide();
    $("#mainone").hide();
    $("#maintwo").hide();
    $("#mainthree").show();
    var myChart = echarts.init(document.getElementById("mainthree"));
    var colors = ['#FF0000'];
    option = {
        backgroundColor: '#071f3a',//背景色
        color: colors,
        tooltip: {
            trigger: 'axis',
            axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                type : 'line'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        legend: {
            show:true,
            //orient:'horizontal',
            //borderWidth:2,
            data:[
                {
                    name:'预报风速',
                    textStyle:{
                        fontSize:12,
                        fontWeight:'bolder',
                        color:'#fff'
                    },
                }
            ]
        },
        toolbox: {
            show: true,
            feature: {
            }
        },
        calculable: false,//取消拖拽
        dataZoom: {
            show: true,
            realtime: true,
            start: 0,
            end: 20
        },
        xAxis: [
            {
                //interval:0,//X轴的刻度间隔
                splitLine: {show: false},//去除网格线
                type: 'category',
                data: date,
                boundaryGap: true,

                //data: date,
                axisLabel: {
                    formatter: '{value}',
                    textStyle: {
                        fontWeight: "bolder",
                        color: "#fff"
                    }
                }
            }
        ],
        yAxis: [
            {
                splitLine:{show: false},//去除网格线
                name: '风速(m/s)',
                min:0,
                nameTextStyle:{
                    color:'#fff'
                },
                type: 'value',
                axisLabel:{
                    formatter: '{value}'/*+'mw'*/,
                    textStyle:{
                        fontWeight:"bolder",
                        color:"#fff"
                    }
                }
            }
        ],
        series: [
            {
                name: '预报风速',
                type: 'line',
                smooth: true,
                yAxisIndex: 0,
                data: foreData,
                //symbol: 'none',  //这句就是去掉点的
                itemStyle: {
                    normal: {
                        lineStyle: {
                            shadowBlur: 10,
                            shadowColor: 'rgba(0, 0, 0, 0.5)',
                            smooth: true,
                            width: 2//线宽
                        }
                    }
                }

            }
        ]
    };
    myChart.setOption(option);
}
//日期转换
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

